package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.SuiweiWatchController;
import com.antoco.operation.bigscreen.model.dto.SuiweiWatchDTO;
import com.antoco.operation.bigscreen.model.entity.SuiweiWatch;
import com.antoco.operation.bigscreen.second.ValidList;
import com.antoco.operation.bigscreen.mapper.SuiweiWatchMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.SuiweiWatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/suiwei-watch")
@RestController
public class SuiweiWatchControllerImpl implements SuiweiWatchController {

    @Autowired
    private SuiweiWatchService suiweiWatchService;

    @Autowired
    private SuiweiWatchMapper suiweiWatchMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping()
    @ResponseStatus(HttpStatus.CREATED)
    public SuiweiWatchDTO save(@RequestBody SuiweiWatchDTO suiweiWatchDTO) {
        SuiweiWatch suiweiWatch = suiweiWatchMapper.asEntity(suiweiWatchDTO);
        suiweiWatchService.save(suiweiWatch);
        //当完成数据的操作后，调用该封装的方法获取变化后的数据进行推送
        channelDataService.pushAllLists(this.getClass().getName());
        return suiweiWatchDTO;
    }

    @Override
    @PostMapping("/save-lists")
    @ResponseStatus(HttpStatus.CREATED)
    public List<SuiweiWatchDTO> saveLists(@Validated @RequestBody ValidList<SuiweiWatchDTO> suiweiWatchDTOLists) {
        suiweiWatchService.save(suiweiWatchMapper.asEntityList(suiweiWatchDTOLists));
        //当完成数据的操作后，调用该封装的方法获取变化后的数据进行推送
        channelDataService.pushAllLists(this.getClass().getName());
        return suiweiWatchDTOLists;
    }

    @Override
    @GetMapping("/{id}")
    public SuiweiWatchDTO findById(@PathVariable("id") String id) {
        SuiweiWatch suiweiWatch = suiweiWatchService.findById(id).orElse(null);
        return suiweiWatchMapper.asDTO(suiweiWatch);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        suiweiWatchService.deleteById(id);
        channelDataService.pushAllLists(this.getClass().getName());
    }

    @Override
    @GetMapping
    public List<SuiweiWatchDTO> list() {
        return suiweiWatchMapper.asDTOList(suiweiWatchService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<SuiweiWatchDTO> pageQuery(Pageable pageable) {
        Page<SuiweiWatch> suiweiWatchPage = suiweiWatchService.findAll(pageable);
        List<SuiweiWatchDTO> dtoList = suiweiWatchPage
                .stream()
                .map(suiweiWatchMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, suiweiWatchPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public SuiweiWatchDTO update(@RequestBody SuiweiWatchDTO suiweiWatchDTO, @PathVariable("id") String id) {
        SuiweiWatch suiweiWatch = suiweiWatchMapper.asEntity(suiweiWatchDTO);
        suiweiWatchMapper.asDTO(suiweiWatchService.update(suiweiWatch, id));
        channelDataService.pushAllLists(this.getClass().getName());
        return suiweiWatchDTO;
    }
}